package com.itheima.web.controller.cargo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.cargo.*;
import com.itheima.service.cargo.ExportService;
import com.itheima.service.cargo.InvoiceService;
import com.itheima.service.cargo.PackingService;
import com.itheima.service.cargo.ShippingOrderService;
import com.itheima.utils.DownloadUtil;
import com.itheima.web.controller.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/cargo/shipping")
public class ShippingController extends BaseController {

    @Reference
    private ShippingOrderService shippingOrderService;
    @Reference
    private PackingService packingService;
    @Reference
    private ExportService exportService;
    @Reference
    private InvoiceService invoiceService;
    @RequestMapping(value = "/list", name = "委托单列表查询")
    public String contractList(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize) {

        ShippingOrderExample shippingOrderExample = new ShippingOrderExample();
        /*shippingOrderExample.setOrderByClause("create_time desc");//按照创建日期排序*/
        PageInfo pageInfo = shippingOrderService.findByPage(pageNum, pageSize, shippingOrderExample);
        request.setAttribute("page", pageInfo);

        return "cargo/shipping/shippingList";
    }
    @RequestMapping(value = "/toShipping", name = "跳转委托单新增页面")
    public String toShipping(String id) {
        //此id设置为装箱单id
        Packing packing = packingService.findById(id);
        String exportIds = packing.getExportIds();
        String[] exportIdArr = exportIds.split(",");
        Export export = exportService.findById(exportIdArr[0]);
        request.setAttribute("export",export);
        request.setAttribute("id", id);
        return "cargo/shipping/shipping-toshipping";
    }
    @RequestMapping(value = "/toUpdate", name = "跳转报运单修改页面")
    public String toUpdate(String id) {
        // 查询当前委托单信息
        ShippingOrder shippingOrder = shippingOrderService.findById(id);
        request.setAttribute("shipping",shippingOrder);
        //此id设置为装箱单id
        Packing packing = packingService.findById(id);
        String exportIds = packing.getExportIds();
        String[] exportIdArr = exportIds.split(",");
        Export export = exportService.findById(exportIdArr[0]);
        request.setAttribute("export",export);
        request.setAttribute("id", id);
        return "cargo/shipping/shipping-toshipping";
    }
    @RequestMapping(value = "/edit", name = "委托单新增或修改")
    public String edit(ShippingOrder shippingOrder,String id) {
        if (StringUtils.isEmpty(shippingOrder.getShippingOrderId())) {
            //设置委托单id
            shippingOrder.setShippingOrderId(id);
            //设置创建人和部门
            shippingOrder.setCheckBy(getLoginUser().getUserName());
            shippingOrder.setCreateBy(getLoginUser().getDeptId());
            shippingOrder.setCreateTime(new Date());
            //委托单状态
            shippingOrder.setState(0);//草稿
            shippingOrderService.save(shippingOrder);
        } else {
            shippingOrderService.update(shippingOrder);
        }
        return "redirect:/cargo/shipping/list.do";
    }
    @RequestMapping(value = "/findShippingState", name = "委托单状态查询")
    public String findShippingState(String id) {
        Invoice invoice = invoiceService.findById(id);
        if (invoice == null){
            ShippingOrder shippingOrder = shippingOrderService.findById(id);
            shippingOrder.setState(0);
            shippingOrderService.update(shippingOrder);
        }
        return "redirect:/cargo/shipping/list.do";
    }
    @RequestMapping(value = "/delete", name = "委托单删除")
    public String delete(String id) {
        shippingOrderService.delete(id);
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping(value = "/shippingExcel", name = "导出Excel委托单")
    public void shippingExcel(String id) throws IOException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-hh");
        //1查询数据
        ShippingOrder shippingOrder = shippingOrderService.findById(id);
        System.out.println(shippingOrder);
        Packing packing = packingService.findById(id);
        System.out.println(packing);
        //2 封装execl
        //2-1) 读取模板文件---工作簿
        String realPath = session.getServletContext().getRealPath("/make/xlsprint/tSHIPPINGORDER.xls");
        Workbook workbook = new HSSFWorkbook(new FileInputStream(new File(realPath)));
        //2-2) 读取工作表
        Sheet sheet = workbook.getSheetAt(0);
        //2-5) 读取, 获取到每个单元格的样式
        Row row3 = sheet.getRow(3);
        row3.getCell(0).setCellValue(shippingOrder.getShipper());
        Row row8 = sheet.getRow(8);
        row8.getCell(0).setCellValue(shippingOrder.getConsignee());
        Row row15 = sheet.getRow(15);
        row15.getCell(0).setCellValue(shippingOrder.getNotifyParty());
        Row row19 = sheet.getRow(19);
        row19.createCell(0).setCellValue(packing.getInvoiceNo());
        String invoiceDate = dateFormat.format(packing.getInvoiceDate());
        row19.createCell(3).setCellValue(invoiceDate);
        row19.createCell(6).setCellValue(shippingOrder.getLcNo());
        Row row23 = sheet.getRow(23);
        row23.createCell(0).setCellValue(shippingOrder.getPortOfLoading());
        if (shippingOrder.getPortOfTrans() != null){
            row23.createCell(3).setCellValue(shippingOrder.getPortOfTrans());
        }
        row23.createCell(6).setCellValue(shippingOrder.getPortOfDischarge());

        String loadingDate = dateFormat.format(shippingOrder.getLoadingDate());
        String limitDate = dateFormat.format(shippingOrder.getLimitDate());
        Row row27 = sheet.getRow(27);
        row27.createCell(0).setCellValue(loadingDate);
        row27.createCell(2).setCellValue(limitDate);
        if ("0".equals(shippingOrder.getIsBatch())){
            row27.createCell(3).setCellValue("否");
        }else {
            row27.createCell(3).setCellValue("是");
        }
        if ("0".equals(shippingOrder.getIsTrans())){
            row27.createCell(5).setCellValue("否");
        }else {
            row27.createCell(5).setCellValue("是");
        }
        row27.createCell(7).setCellValue(shippingOrder.getCopyNum());
        Row row31 = sheet.getRow(31);
        row31.createCell(0).setCellValue(packing.getMarks());
        row31.createCell(3).setCellValue(packing.getDescriptions());

        Row row37 = sheet.getRow(37);
        row37.createCell(1).setCellValue(shippingOrder.getSpecialCondition());
        Row row43 = sheet.getRow(43);
        row43.createCell(7).setCellValue(shippingOrder.getCheckBy());
        //3 下载
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        workbook.write(byteArrayOutputStream);
        DownloadUtil.download(byteArrayOutputStream,response,"委托表.xls");
    }
}
